// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import zombie.debug.DebugLog;

public final class ConfigFile {
    protected ArrayList<ConfigOption> options;
    protected int version;

    private void fileError(String string1, int _int, String string0) {
        DebugLog.log(string1 + ":" + _int + " " + string0);
    }

    public boolean read(String fileName) {
        this.options = new ArrayList();
        this.version = 0;
        File file = new File(fileName);
        if (!file.exists()) {
            return false;
        } else {
            DebugLog.log("reading " + fileName);

            try {
                FileReader fileReader = new FileReader(file);

                try {
                    BufferedReader bufferedReader = new BufferedReader(fileReader);

                    try {
                        int _int = 0;

                        while (true) {
                            String string0 = bufferedReader.readLine();
                            if (string0 == null) {
                                break;
                            }

                            _int++;
                            string0 = string0.trim();
                            if (!string0.isEmpty() && !string0.startsWith("#")) {
                                if (!string0.contains("=")) {
                                    this.fileError(fileName, _int, string0);
                                } else {
                                    String[] string1 = string0.split("=");
                                    if ("Version".equals(string1[0])) {
                                        try {
                                            this.version = Integer.parseInt(string1[1]);
                                        } catch (NumberFormatException numberFormatException) {
                                            this.fileError(fileName, _int, "expected version number, got \"" + string1[1] + "\"");
                                        }
                                    } else {
                                        StringConfigOption stringConfigOption = new StringConfigOption(string1[0], string1.length > 1 ? string1[1] : "", -1);
                                        this.options.add(stringConfigOption);
                                    }
                                }
                            }
                        }
                    } catch (Throwable throwable0) {
                        try {
                            bufferedReader.close();
                        } catch (Throwable throwable1) {
                            throwable0.addSuppressed(throwable1);
                        }

                        throw throwable0;
                    }

                    bufferedReader.close();
                } catch (Throwable throwable2) {
                    try {
                        fileReader.close();
                    } catch (Throwable throwable3) {
                        throwable2.addSuppressed(throwable3);
                    }

                    throw throwable2;
                }

                fileReader.close();
                return true;
            } catch (Exception exception) {
                exception.printStackTrace();
                return false;
            }
        }
    }

    public boolean write(String fileName, int _version, ArrayList<? extends ConfigOption> _options) {
        File file = new File(fileName);
        DebugLog.log("writing " + fileName);

        try {
            FileWriter fileWriter = new FileWriter(file, false);

            try {
                if (_version != 0) {
                    fileWriter.write("Version=" + _version + System.lineSeparator());
                }

                for (int _int = 0; _int < _options.size(); _int++) {
                    ConfigOption configOption = (ConfigOption)_options.get(_int);
                    String string = configOption.getTooltip();
                    if (string != null) {
                        string = string.replaceAll("\n", System.lineSeparator() + "# ");
                        fileWriter.write("# " + string + System.lineSeparator());
                    }

                    fileWriter.write(
                        configOption.getName()
                            + "="
                            + configOption.getValueAsString()
                            + (_int < _options.size() - 1 ? System.lineSeparator() + System.lineSeparator() : "")
                    );
                }
            } catch (Throwable throwable0) {
                try {
                    fileWriter.close();
                } catch (Throwable throwable1) {
                    throwable0.addSuppressed(throwable1);
                }

                throw throwable0;
            }

            fileWriter.close();
            return true;
        } catch (Exception exception) {
            exception.printStackTrace();
            return false;
        }
    }

    public ArrayList<ConfigOption> getOptions() {
        return this.options;
    }

    public int getVersion() {
        return this.version;
    }
}
